﻿var MONITORING_SERVER_URL = 'http://localhost:13694/';
var pageViewInfo;
var pageId;

$(function () {

    var serverHub = $.connection.serverHub;

    serverHub.onOpenConnection = function (browserName, userIP, userHostName, startDate) {
        updateOnlineUser([{
            browserName: browserName,
            userIP: userIP,
            userHostName: userHostName,
            startDate: startDate
        }]);
    };

    serverHub.onPageView = function (domain, url, clientUsername, serverUsername, startDate) {
        updatePageView([{
            domain: domain,
            url: url,
            clientUsername: clientUsername,
            serverUsername: serverUsername,
            startDate: startDate
        }]);
    };

    $.connection.hub.start().done(function () {

        serverHub.getLiveConnections().done(function (x) {
            updateOnlineUser(x);
        });

    });

    //    serverHub.onReconnectConnection = function (connectionId) {
    //        //        alert('onReconnectConnection');
    //    };

    //    serverHub.onCloseConnection = function (connectionId) {
    //        //        alert('onCloseConnection');
    //    };

    //*******************************************************************************************  Online User Update fun
    var updateOnlineUser = function (jsonData) {
        var onlineUserTable = $("#onlineUserContainerBox > table");
        for (user in jsonData) {
            var userRow = $("<tr></tr>");
            for (d in jsonData[user]) {
                userRow.append($("<td>{0}</td>".format(jsonData[user][d])));
            }
            onlineUserTable.prepend(userRow);
        };
    };

    //*******************************************************************************************  Page View Update fun
    var updatePageView = function (jsonData) {
        var pageViewTable = $("#pageViewContainerBox > table");
        for (page in jsonData) {
            var pageRow = $("<tr></tr>").hide();
            for (d in jsonData[page]) {
                pageRow.append($("<td>{0}</td>".format(jsonData[page][d])));
            }
            pageViewTable.prepend(pageRow);
            pageRow.slideDown('slow');
        };
    };
    //*******************************************************************************************  domain List Update fun
    var updateDomainList = function (jsonData) {
        var domainListContainer = $("#domainListContainerBox");
        domainListContainer.empty();
        for (domain in jsonData) {
            var domainRow = $("<div class='domain-row' > <span class='domain-title' >{0}</span> <span class='domain-exeption-count' >{2}</span> <span class='domain-visitor-count' >{1}</span>  </div>".format(jsonData[domain].domain, jsonData[domain].exeptionCount, jsonData[domain].visitorCount));
            domainListContainer.append(domainRow);
        }
    };
    // Test Function, Pls Remove if does not need
    updateDomainList([{
        domain: "http://hrm.bankemellat.ir",
        visitorCount: 5,
        exeptionCount: 3
    }, {
        domain: "http://tajan.ir",
        visitorCount: 5,
        exeptionCount: 3
    }]);

    $("#domainListTestBtn").click(function () {
        updateDomainList([{
            domain: "http://hrm.bankemellat.ir",
            visitorCount: getRandomInt(),
            exeptionCount: getRandomInt()
        }, {
            domain: "http://tajan.ir",
            visitorCount: getRandomInt(),
            exeptionCount: getRandomInt()
        }, {
            domain: "http://sits-co.ir",
            visitorCount: getRandomInt(),
            exeptionCount: getRandomInt()
        }]);

    })
})